package com.naodong.shenluntiku.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerLoginComponent;
import com.naodong.shenluntiku.di.module.LoginModule;
import com.naodong.shenluntiku.mvp.contract.LoginContract;
import com.naodong.shenluntiku.mvp.model.bean.LoginInfo;
import com.naodong.shenluntiku.mvp.model.data.manager.TokenManager;
import com.naodong.shenluntiku.mvp.presenter.LoginPresenter;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import butterknife.OnClick;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.activitymanager.TheActivityManager;
import me.shingohu.man.util.SnackbarUtil;
import me.shingohu.man.util.ViewUtil;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */

/**
 * Created by hujie on 17/5/8.
 */

public class LoginActivity extends BaseMVPActivity<LoginPresenter> implements LoginContract.View {


    @BindView(R.id.userNameTV)
    EditText userNameTV;
    @BindView(R.id.passwordTV)
    EditText passwordTV;


    @AutoBundleField(required = false)
    boolean relogin;//是否重新登录

    @Override
    public void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        checkRelogin();
    }


    @OnClick(R.id.button)
    public void onLogin() {
        if (checkInput()) {
           // mPresenter.login(ViewUtil.getText(userNameTV), ViewUtil.getText(passwordTV));
        }
    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    private boolean checkInput() {
        if (ViewUtil.isTextEmpty(userNameTV)) {
            SnackbarUtil.makeSnack(getRootView(), "请输入用户名");
            return false;
        }
        if (ViewUtil.isTextEmpty(passwordTV)) {
            SnackbarUtil.makeSnack(getRootView(), "请输入密码");
            return false;
        }
        return true;
    }


    private void checkRelogin() {
        if (relogin) {
            TokenManager.getInstance().clearToken();
            TheActivityManager.getInstance().toInstanceOf(LoginActivity.class);
        }
    }

    @Override
    protected int layoutId() {
        return R.layout.a_login;
    }


    @Override
    public void showMessage(String msg) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        checkRelogin();
    }

    @Override
    public boolean showBackNavication() {
        return false;
    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }

    @Override
    public void onLoginSuccess(LoginInfo token) {
        Intent intent = new Intent(mActivity, MainActivity.class);
        startActivity(intent);
        finish();
    }
}